<template>
  <div id="img">
    <div class="Mycarousel" v-for="(item,index) in rotation.menu_imt" ref="img" :key="item.id"
         :class="{show:index===imgIndex}">
      <img :src="'/api'+item.url" alt="">
    </div>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "Carousel",
  data() {
    return {
      imgIndex: 0,
    }
  },
  mounted() {
    // 1表示首页,2表示新闻页，3表示文章详情页
    this.timer = setInterval(() => {
      this.imgIndex += 1
      if (this.imgIndex >= this.rotation.menu_imt.length) {
        this.imgIndex = 0
      }
    }, this.rotation.menu_time * 1000)
    // 这里定时器通过跳转到文章详情页面需要及时关闭,暂未解决
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer) // 关闭循环器
    }
  },
  computed: {
    ...mapState('Rotation', ['rotation'])
  }
}
</script>

<style scoped lang="less">
/*轮播图*/
#img {
  position: relative;
  width: 100%;
  height: 700px;


  .Mycarousel {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    Overflow: hidden;
    transition: all 3s;
    opacity: 0;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .show {
    opacity: 1;
  }
}
</style>